package ma.platform.server.student.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import ma.plaform.server.common.log.annotation.SysLog;
import ma.platform.server.common.security.annotation.HasPermission;
import ma.platform.server.core.constant.CacheConstants;
import ma.platform.server.core.util.R;
import ma.platform.server.student.entity.TbStudent;
import ma.platform.server.student.entity.TbStudentParent;
import ma.platform.server.student.req.QueryStudentReq;
import ma.platform.server.student.service.TbStudentParentService;
import ma.platform.server.student.service.TbStudentService;
import ma.platform.server.student.vo.StudentExcelVo;
import ma.platform.server.upms.api.entity.SysDict;
import ma.platform.server.upms.api.vo.RoleExcelVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/student")
@Tag(description = "student", name = "学生管理模块")
public class StudentController {

    @Resource
    private TbStudentService studentService;

    @Resource
    private TbStudentParentService studentParentService;

    @GetMapping("/page")
    public R pageStudent(@ParameterObject Page page, @ParameterObject QueryStudentReq req) {
        return R.ok(studentService.pageList(page, req));
    }

    /**
     * 添加学生
     *
     * @return success、false
     */
    @SysLog("添加学生")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('tb_student_add')")
    public R save(@Valid @RequestBody TbStudent tbStudent) {
        studentService.save(tbStudent);
        return R.ok(tbStudent);
    }

    /**
     * 删除学生
     *
     * @param ids ID
     * @return R
     */
    @SysLog("删除学生")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('tb_student_del')")
    @Transactional(rollbackFor = Exception.class)
    public R removeById(@RequestBody Long[] ids) {
        studentParentService.remove(Wrappers.<TbStudentParent>lambdaQuery().in(TbStudentParent::getStudentId, ids));
        return R.ok(studentService.removeByIds(Lists.newArrayList(ids)));
    }

    /**
     * 修改学生
     *
     * @return success/false
     */
    @PutMapping
    @SysLog("修改学生")
    @PreAuthorize("@pms.hasPermission('tb_student_edit')")
    public R updateById(@Valid @RequestBody TbStudent student) {
        studentService.updateById(student);
        return R.ok();
    }


    /**
     * 通过ID查询信息
     *
     * @param id ID
     */
    @GetMapping("/details/{id}")
    public R getById(@PathVariable Long id) {
        return R.ok(studentService.getById(id));
    }

    /**
     * 导入角色
     *
     * @param excelVOList   角色列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    public R importRole(@RequestExcel List<StudentExcelVo> excelVOList, BindingResult bindingResult) {
        return studentService.importRole(excelVOList, bindingResult);
    }

}

